
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        :before-close="beforeClose"
      >
       <template slot-scope="scope" slot="applyTypeForm">
          <avue-select
            v-model="form.applyType"
            type="tree"
            :dic="dicAdd"
            :props="dicAddProps"
            disabled
          />
        </template>
        <template slot="menuLeft">
          <el-popover v-if="shenpi_btn" placement="bottom" width="260" v-model="batchReviewVisible">
            <div style="text-align: center; margin: 0">
              <el-button size="mini" type="primary" @click="auditO(2)">审批通过</el-button>
              <el-button type="primary" size="mini" @click="auditO(3)">审批不通过</el-button>
            </div>
            <el-button type="primary" size="small" slot="reference">批量审批</el-button>
          </el-popover>

          <el-button type="primary" v-if="tijiao_btn" size="small" @click="submitO">提交</el-button>
          <el-button type="primary" v-if="tijiao_btn" size="small" @click="del">删除</el-button>
          <el-upload
            class="upload-inline"
            action
            :show-file-list="false"
            accept=".xls, .xlsx"
            :http-request="handleImport"
          >
            <el-button type="primary" size="small">导入</el-button>
          </el-upload>
          <el-button type="primary" size="small" @click="exportClick">导出</el-button>
        </template>

        <template slot-scope="scope" slot="menu">
          <el-button
            :type="scope.type"
            :size="scope.size"
            v-if="scope.row.applyStatus == '0' || scope.row.applyStatus == '3'"
            @click="openEdit(scope.row,scope.index)"
          >编辑</el-button>
          <el-button
            :type="scope.type"
            :size="scope.size"
            @click.stop="openView(scope.row,scope.index)"
          >预览</el-button>
          </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {dateFormat} from '@/util/date'
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  submitBatch,
  auditBatch,
  exportExcel,
  saveBatch,
  delBatch,
  dataExport
} from "@/api/subway/swtkofficialproduction";
import { tableOption,tableOptionView,tableOptionEdit,startDate, endDate } from "@/const/crud/subway/swtkofficialproduction";
import {
 remote
} from "@/api/admin/dict";
import { handleExcelResponse } from "@/util/util";

export default {
  name: "swtkofficialproduction",
  mixins: [baseCrud('subway/swtkofficialproduction')],
  data() {
    return {
      dicAdd:[],
      tableOptionView: tableOptionView,
      tableOptionEdit: tableOptionEdit,
      searchForm: {
        dates1: [startDate, endDate]
      },
      newData: null
    };
  },
  // 自定义按钮权限初始化
  created() {
    this.shenpi_btn = this.permissions["st_swtkofficialproduction_auditBatch"];
    this.tijiao_btn = this.permissions["st_swtkofficialproduction_submitBatch"];
    this.shanchu_btn = this.permissions["st_swtkofficialproduction_delBatch"];
    this.newData = JSON.parse(JSON.stringify(tableOption));
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.st_swtkofficialproduction_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.st_swtkofficialproduction_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.st_swtkofficialproduction_edit,
          false
        )
      };
    }
  },
  methods: {
    beforeClose(done,type){
      if (["view", "edit"].includes(type)) {
        this.tableOption = this.newData;
      }
      done();
    },
    openView(row, index) {
      this.tableOption = tableOptionView;
      this.$refs.crud.rowView(row, index);
      return;
    },
    openEdit(row, index) {
      this.tableOption = tableOptionEdit;
      this.$refs.crud.rowEdit(row, index);
    },
    handleImport(item) {
      let file = item.file;
      this.$message.info("正在导入，请耐心等待！");
      let param = new FormData();
      param.append("file", file);
      saveBatch(param).then(() => {
        this.$notify({
          title: "成功",
          message: "导入成功",
          type: "success",
          duration: 2000
        });
        this.getList(this.page);
      });
    },
    del() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.applyStatus != 0) {
          this.$message.error("请检查所选数据，只有未提交的数据可以删除");
          return false;
        }
        if (table.applyStatus == 0) {
          ids.push(table.id);
        }
      }
      delBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("删除成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    auditO(s) {
      let loadingInstance1 = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        loadingInstance1.close();
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.applyStatus != "1" && table.applyStatus != "3") {
          this.$message.error("请检查勾选项，只能审批未审核或者审核不通过的数据。");
          loadingInstance1.close();
          return false;
        }
        table.applyStatus = s;
        ids.push(table);
      }
      auditBatch(ids)
        .then(data => {
          loadingInstance1.close();
          this.$message.success("操作成功");
          this.getList(this.page);
          this.batchReviewVisible = false;
        })
        .catch(() => {
          loadingInstance1.close();
        });
    },
    submitO() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.applyStatus != "0" && table.applyStatus != "3") {
          this.$message.error("请检查勾选项，只能提交未提交或者审批不通过的数据。");
          return false;
        }
        ids.push(table);
      }
      submitBatch(ids)
        .then(data => {
          this.$message.success("提交成功");
          this.getList(this.page);
        })
        .catch(() => {
          loadingInstance1.close();
        });
    },
    getList(page, params) {
      this.tableLoading = true;
      if(this.shenpi_btn){
      return fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
            roleSH:'0'
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
        }else{
             return fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
        }
    },
    beforeOpen(done, type) {

      if (["view", "edit"].includes(type)) {
        // 查看和编辑逻辑
        setTimeout(() => {
             remote("st_sw_tk_official_ticket_product_apply").then(response=>{
                this.dicAdd=response.data.data
            })
        }, 0);
      } else {
        //新增逻辑
        setTimeout(() => {
              remote("st_sw_tk_official_ticket_product_apply").then(response=>{
                this.dicAdd=response.data.data
            })
          this.form.applyType = "0";
          this.form.applyTime=dateFormat(new Date());
        }, 0);
      }
      done();
    },
    //导出
    async exportClick (row) {
      this.dataExport();
    },
    async dataExport () {
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await dataExport(Object.assign( this.searchForm ));
        handleExcelResponse(res);
      } finally {
        loading.close();
      }
    },
  }
};
</script>
